var __PAGE_SIZE = 15;

axios.defaults.timeout = 50000;
// axios.defaults.baseURL = "/api";
axios.interceptors.response.use(function(response){
    return Promise.resolve(response.data);
}, function (error) {
    var errResponse = error.response;
    if(errResponse) {
        if(errResponse.status === 401) {
            errMsg = '会话已过期，请重新登录';
            location.href = "/login";
        } else {
            errMsg = errResponse.data.message;
        }
    } else {
        errMsg = '程序开小差了，请稍后再试';
    }
    Vue.prototype.$message({
        showClose: true,
        message: errMsg,
        type: 'error'
    });
    return Promise.reject(error.response);
});

var G = {
    tid: {
        _count: 1,
        get(){return ((+new Date()) + "_" + (this._count++))}
    },
    error: function(errMsg) {
        Vue.prototype.$message({
            showClose: true,
            message: errMsg,
            type: 'error'
        });
    },
    findIndex: (row, data) => {
        let index = data.findIndex(item => {
            if(row.id) {
                return row.id === item.id;
            } else {
                return row.key === item.key
            }
        });
        return index;
    },
    activeNav: function(id) {
        if(id) {
            var navEl = document.getElementById(id);
            navEl.style.color = "rgb(255, 208, 75)";
        }
    },
    req: {
        query: function() {
            var pairs = window.location.search.substring(1).split("&"),
                obj = {},
                pair,
                i;
            for ( i in pairs ) {
                if ( pairs[i] === "" ) continue;
                pair = pairs[i].split("=");
                obj[ decodeURIComponent( pair[0] ) ] = decodeURIComponent( pair[1] );
            }
            return obj;
        }
    }
};

Storage.prototype.put = function(key, value) {
    this.setItem(key, JSON.stringify(value));
};
Storage.prototype.get = function(key) {
    var value = this.getItem(key);
    return value && JSON.parse(value);
};
Date.prototype.format = function(fmt) {
    var o = {
        "M+" : this.getMonth()+1,                 //月份
        "d+" : this.getDate(),                    //日
        "H+" : this.getHours(),                   //小时
        "m+" : this.getMinutes(),                 //分
        "s+" : this.getSeconds(),                 //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S"  : this.getMilliseconds()             //毫秒
    };
    if(/(y+)/.test(fmt)) {
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    for(var k in o) {
        if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
    return fmt;
};

